package per.qiao.simplerest;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import per.qiao.simplerest.core.RestServiceAutoConfig;
import per.qiao.simplerest.restservice.PersonDto;
import per.qiao.simplerest.restservice.ResponseDto;
import per.qiao.simplerest.restservice.TestService;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@Import(RestServiceAutoConfig.class)
public class SimpleRestApplication {

    @Autowired
    TestService testService;

    public List<PersonDto> findAll() {
        return null;
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SimpleRestApplication.class, args);
        TestService bean = applicationContext.getBean(TestService.class);



//        findById(bean);
//        restfulTest(bean);
//        findAll(bean);
        findAll2(bean);
//        test6();
//        createPerson(bean);
//        createPerson2(bean);
//        createPerson3(bean);

//        updatePerson(bean);
//        updatePerson2(bean);
//        updatePerson3(bean);

//        deleteById(bean);
//        deleteById2(bean);
//        deleteById3(bean);
    }

    public static void test6(){
        String url = "http://localhost:8080/simpleRest/findAll2";
//        Map<String, Object> paramMap = Maps.newHashMap();
//        paramMap.put("xx", "xx");

        RestTemplate restTemplate = new RestTemplate();

        HttpEntity<?> httpEntity = new HttpEntity<>(null, null);
        Method findAll2 = null;
        try {
            findAll2 = TestService.class.getMethod("findAll2");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Type genericReturnType = findAll2.getGenericReturnType();
//        ResponseEntity<ResponseDto<List<PersonDto>>> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, new ParameterizedTypeReference<ResponseDto<List<PersonDto>>>() {});
        ParameterizedTypeReference<ResponseDto> objectParameterizedTypeReference = ParameterizedTypeReference.forType(genericReturnType);
        ResponseEntity<ResponseDto> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, objectParameterizedTypeReference);
        ResponseDto body = result.getBody();
        System.out.println(body);
    }

    /**
     * GET
     */
    public static void findById(TestService bean) {
        PersonDto byId = bean.findById(100L);
        System.out.println(byId);
    }
    public static void restfulTest(TestService bean) {
        String qiaogege = bean.restfulTest("qiaogege", 26);
        System.out.println(qiaogege);
    }
    public static void findAll(TestService bean) {
        List<PersonDto> all = bean.findAll();
        all.forEach(System.out::print);
    }
    public static void findAll2(TestService bean) {
        ResponseDto<List<PersonDto>> responseDto = bean.findAll2();
        List<PersonDto> data = responseDto.getData();
        data.forEach(System.out::println);
    }

    public static void createPerson(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        PersonDto person = bean.createPerson(personDto);
        System.out.println(person);
    }

    public static void createPerson2(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        PersonDto person = bean.createPerson2(personDto, "hello world");
        System.out.println(person);
    }

    public static void createPerson3(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        PersonDto person = bean.createPerson3(personDto, "hello world");
        System.out.println(person);
    }

    /**
     * PUT
     */

    public static void updatePerson(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        bean.updatePerson(personDto);
    }
    public static void updatePerson2(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        bean.updatePerson2(personDto, "updatePerson2");
    }
    public static void updatePerson3(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("qiao");
        personDto.setBirthday(new Date());
        bean.updatePerson3(personDto, "updatePerson3");
    }


    /**
     * DELETE
     */
    public static void deleteById(TestService bean) {
        bean.deleteById(100L);
    }
    public static void deleteById2(TestService bean) {
        bean.deleteById2(50L);
    }
    public static void deleteById3(TestService bean) {
        PersonDto personDto = new PersonDto();
        personDto.setName("那谁...");
        personDto.setBirthday(new Date());
        bean.deleteByDto(personDto);
    }
}
